package com.palrevo.beackBeans.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

import com.palrevo.beackBeans.BeansBase;
import com.palrevo.common.commands.Command;
import com.palrevo.controllers.MsgsController;
import com.palrevo.controllers.NavigationController;
import com.palrevo.controllers.NavigationRule;
import com.palrevo.data.dao.DataException;
import com.palrevo.data.models.Page;
import com.palrevo.data.services.PagesDBService;
import com.palrevo.utils.TreeDragDropEvent;
import com.palrevo.utils.WebUtils;

/**
 * @author AAsha
 *
 */
@SuppressWarnings("serial")
@ManagedBean(name="pagesBean")
@SessionScoped
public class PagesBean extends BeansBase {
	
	private Map<Long, Page> pagesSummary = new HashMap<Long, Page>();
	private Page page = new Page();
	
	
	//----------------------------------
	private TreeNode root;  
    
    private TreeNode root2;  
      
    private TreeNode selectedNode;  
  
    private TreeNode selectedNode2; 
    
    public TreeNode getRoot() {  
        return root;  
    }  
  
    public TreeNode getRoot2() {  
        return root2;  
    }  
      
    public TreeNode getSelectedNode() {  
        return selectedNode;  
    }  
    public void setSelectedNode(TreeNode selectedNode) {  
        this.selectedNode = selectedNode;  
    }  
  
    public TreeNode getSelectedNode2() {  
        return selectedNode2;  
    }  
    public void setSelectedNode2(TreeNode selectedNode2) {  
        this.selectedNode2 = selectedNode2;  
    }  
  
    public void onDragDrop(TreeDragDropEvent event) {  
        TreeNode dragNode = event.getDragNode();  
        TreeNode dropNode = event.getDropNode();  
        int dropIndex = event.getDropIndex();  
         
        MsgsController.addInfoMessage("Dragged " + dragNode.getData() + ", Dropped on " + dropNode.getData() + " at " + dropIndex);
    } 
    
    public void prepareMenu(){
    	root = new DefaultTreeNode("Root", null);
        TreeNode node0 = new DefaultTreeNode("Node 0", root);  
        TreeNode node1 = new DefaultTreeNode("Node 1", root);  
        TreeNode node2 = new DefaultTreeNode("Node 2", root);  
          
        TreeNode node00 = new DefaultTreeNode("Node 0.0", node0);  
        TreeNode node01 = new DefaultTreeNode("Node 0.1", node0);  
          
        TreeNode node10 = new DefaultTreeNode("Node 1.0", node1);  
        TreeNode node11 = new DefaultTreeNode("Node 1.1", node1);  
          
        TreeNode node000 = new DefaultTreeNode("Node 0.0.0", node00);  
        TreeNode node001 = new DefaultTreeNode("Node 0.0.1", node00);  
        TreeNode node010 = new DefaultTreeNode("Node 0.1.0", node01);  
          
        TreeNode node100 = new DefaultTreeNode("Node 1.0.0", node10);  
          
        root2 = new DefaultTreeNode("Root2", null);  
        TreeNode item0 = new DefaultTreeNode("Item 0", root2);  
        TreeNode item1 = new DefaultTreeNode("Item 1", root2);  
        TreeNode item2 = new DefaultTreeNode("Item 2", root2);  
          
        TreeNode item00 = new DefaultTreeNode("Item 0.0", item0); 
        
        NavigationController.navigate(NavigationRule.MENU_EDITOR);
    }
    
    //----------------------------------
    
    
	
	public PagesBean() {
	}
	
	
	public void preparePagesSummary() throws DataException{
		
		pagesSummary = PagesDBService.getInstance().loadContentPages();
		
		NavigationController.navigate(NavigationRule.PAGES_SUMMARY);
	}
	
	public void preCreatePage(){
		this.page = new Page();
		NavigationController.navigate(NavigationRule.CREATE_PAEG);
	}
	
	/**
	 * 
	 * @throws DataException
	 */
	public void createPage() throws DataException{
		
		PagesDBService.getInstance().addPage(page);
		pagesSummary.put(page.getId(), page);
		
		MsgsController.addInfoMessage("تم اضافة الصفحة بنجاح");
		
		NavigationController.navigate(NavigationRule.PAGES_SUMMARY);
	}
	
	public void preEditPage(Page p){
		this.page = pagesSummary.get(p.getId());
		NavigationController.navigate(NavigationRule.EDIT_PAEG);
	}
	
	public void editPage() throws DataException{
		
		PagesDBService.getInstance().updatePage(page);
		MsgsController.addInfoMessage("تم تحديث الصفحة بنجاح");
		
		NavigationController.navigate(NavigationRule.PAGES_SUMMARY);
	}
	
	public void preDeletePage(Page p){
		
		this.page = pagesSummary.get(p.getId());
		
		Command confirm = new Command(){
			@Override
			public void execute() throws Exception {
				deletePage();
			}
		};
		
		Command cancel = new Command(){
			@Override
			public void execute() throws Exception {
				NavigationController.navigate(NavigationRule.PAGES_SUMMARY);
			}
		};
		
		WebUtils.getAppSessionBean().setConfirm(confirm);
		WebUtils.getAppSessionBean().setCancel(cancel);
		
		MsgsController.addWarnMessage("تاكيد عملية الحذف !");
		
		NavigationController.navigate(NavigationRule.CONFIRMATION);
	}
	
	private void deletePage() throws DataException{
		
		PagesDBService.getInstance().deletePage(page);
		MsgsController.addInfoMessage("تم حذف الصفحة بنجاح");
		
		pagesSummary.remove(page.getId());
		
		NavigationController.navigate(NavigationRule.PAGES_SUMMARY);
	}
	
	
	//====================================
	// ====== Setters and Getters ========
	//====================================
	
	/**
	 * @return the pagesSummary
	 */
	public ArrayList<Page> getPagesSummary() {
		return new ArrayList<Page>(pagesSummary.values());
	}


	/**
	 * @return the page
	 */
	public Page getPage() {
		return page;
	}


	/**
	 * @param page the page to set
	 */
	public void setPage(Page page) {
		this.page = page;
	}
	
}
